package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CouresePublishVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.DIYException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-15
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    /**
     * 添加课程信息，是同时向两张表中添加
     * 课程表 课程描述表
     * @param courseInfoVo
     */
    @Autowired
    private EduCourseDescriptionService descriptionService;
    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;

    @Override
    public void saveCourseInfo(CourseInfoVo courseInfoVo) {

        //1 向课程表中添加
        //CourseInfoVo对象转换为eduCourse
        EduCourse eduCourse = new EduCourse();
        //BeanUtils.copyProperties该方法将courseInfoVo复制到eduCourse
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert<=0){
            throw new DIYException(20001,"添加课程失败");
        }
        //获取添加之后课程id
        String id = eduCourse.getId();
        //2 向课程简介表添加简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(id);

        descriptionService.save(eduCourseDescription);

    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        //2 查询简介表
        EduCourseDescription eduCourseDescription = descriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }
    //修改课程
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if (i<=0){
            throw new DIYException(20001,"修改课程信息失败");
        }
        //修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        descriptionService.updateById(eduCourseDescription);
    }


    @Override
    public CouresePublishVo getPublishCourseInfo(String courseId) {

        return baseMapper.getPublishCourseInfo(courseId);
    }
    //删除课程，多表
    @Override
    public void removeCourse(String courseId) {
        //1 .根据课程id删除小节video
        videoService.removeVideoByCourseId(courseId);

        //2 .根据课程id删除章节chapter
        chapterService.removeChapterByCourseId(courseId);

        //3.根据课程id删除描述description
        descriptionService.removeById(courseId);

        //4.根据课程id删除课程本身course
        int i = baseMapper.deleteById(courseId);
        if (i==0){
            throw new DIYException(20001,"failed");
        }
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {

        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            //一级分类  查询
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            //二级分类  查询
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            //关注度  销量排序
           wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            //最新 创建时间排序
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            //最新  价格排序
            wrapper.orderByDesc("price");
        }

       baseMapper.selectPage(coursePage, wrapper);
        List<EduCourse> records = coursePage.getRecords();//数据
        long current = coursePage.getCurrent();//当前页
        long pages = coursePage.getPages();//总页数
        long size = coursePage.getSize();//每页记录时
        long total = coursePage.getTotal();//总记录时
        //当前是否有下一页
        boolean hasNext = coursePage.hasNext();
        //当前是否有上一页
        boolean hasPrevious = coursePage.hasPrevious();

        //把分页数据获取 放到map集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //根据课程id ，编写sql，语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
